﻿using System.Windows.Media.Media3D;

namespace LFSRecord2.Model
{
    public class CompLayerMeshBase
    {
        public CompLayerTypes LayerType;

        public int NumPoints { get; set; }
        public Point3D[] Points;
        public int NumLineIndexes { get; set; }
        public int[] LineIndexes;

        public Point3D CenterPoint { get; set; }
        public Point3D LookAtPoint { get; set; }

        public CompLayerMeshBase()
        {
            CenterPoint = new Point3D();
            LookAtPoint = new Point3D();
        }

        public CompLayerMeshBase CreateTransformedMesh(Matrix3D mat, Matrix3D viewportViewMatrix, Vector3D viewportScale)
        {
            mat = Matrix3D.Multiply(mat, viewportViewMatrix);

            // Create a new object, so we can return a processed copy of ourselves
            CompLayerMeshBase objectMesh = new CompLayerMeshBase();
            objectMesh.NumPoints = NumPoints;
            objectMesh.Points = (Point3D[])Points.Clone();
            objectMesh.NumLineIndexes = NumLineIndexes;
            objectMesh.LineIndexes = LineIndexes;           // we can just reference this array because we don't touch it anywhere

            objectMesh.CenterPoint = mat.Transform(CenterPoint);
            objectMesh.LookAtPoint = mat.Transform(LookAtPoint);

            // Remove position before adding viewport scale, then add position again (only for cameras, to keep camera mesh size constant)
            if (LayerType != CompLayerTypes.Null)
            {
                Point3D offset = new Point3D(mat.OffsetX, mat.OffsetY, mat.OffsetZ);
                mat.OffsetX = 0;
                mat.OffsetY = 0;
                mat.OffsetZ = 0;
                mat.Scale(viewportScale);
                mat.OffsetX = offset.X;
                mat.OffsetY = offset.Y;
                mat.OffsetZ = offset.Z;
            }

            // Transform the object mesh (camera or null rectangle)
            mat.Transform(objectMesh.Points);

            return objectMesh;
        }
    }
}
